﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class OperatorFullView : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] roles = Roles.GetRolesForUser(Page.User.Identity.Name);

                if (!roles.Contains("Manager"))
                {
                    Response.Redirect("Default.aspx");
                }

                int staffId;
                if (int.TryParse(Request.QueryString["staffId"], out staffId))
                {
                    LoadDetailsView(staffId);
                }
                else
                {
                    Response.Redirect("Operators.aspx");
                }
            }
        }

        private void LoadDetailsView(int staffId)
        {
            var staff = StaffBLL.GetStaffById(staffId);
            var admin = AdministratorBLL.GetAdministrator(staffId);
            var administrator = new Administrator(staff)
            {
                Role = admin.Role,
                UserCredentials = admin.UserCredentials
            };

            DetailsView1.DataSource = new List<Administrator> {administrator};
            DetailsView1.DataBind();
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            Response.Redirect("EditOperatorForm.aspx?staffId=" + Request.QueryString["staffId"]);
        }
    }
}